package com.coursebooking.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.coursebooking.model.Booking;
import com.coursebooking.model.Review;
import com.coursebooking.model.Student;
import com.coursebooking.model.User;
import com.coursebooking.repository.BookingRepository;
import com.coursebooking.repository.ReviewRepository;
import com.coursebooking.repository.StudentRepository;
import com.coursebooking.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class StudentService{

    @Autowired
    private StudentRepository studentRepository;
    
    @Autowired
    private BookingRepository bookingRepository;
    
    @Autowired
    private ReviewRepository reviewRepository;
    @Autowired
    private UserRepository userRepository;

    public Student createStudent(String name, String studentId, String email, String phone, String grade) {
        if (studentRepository.existsByStudentId(studentId)) {
            throw new RuntimeException("学号已存在");
        }
        if (studentRepository.existsByEmail(email)) {
            throw new RuntimeException("学生邮箱已存在");
        }

        Student student = new Student(name, studentId, email, phone, grade);
        studentRepository.insert(student);
        return student;
    }
   public Student createStudent(Student student) {
       String studentId = student.getStudentId();
       String email = student.getEmail();
       if (studentRepository.existsByStudentId(studentId)) {
            throw new RuntimeException("学号已存在");
        }
        if (studentRepository.existsByEmail(email)) {
            throw new RuntimeException("学生邮箱已存在");
        }
        studentRepository.insert(student);
        return student;
    }

    public Optional<Student> findById(Long id) {
        return Optional.ofNullable(studentRepository.selectById(id));
    }

    public Optional<Student> findByStudentId(String studentId) {
        return studentRepository.findByStudentId(studentId);
    }

    public Optional<Student> findByEmail(String email) {
        return studentRepository.findByEmail(email);
    }

    public List<Student> findAll() {
        return studentRepository.selectList(null);
    }

    public Student updateStudent(Student student) {
        if (studentRepository.selectById(student.getId()) == null) {
            throw new RuntimeException("学生不存在");
        }
        studentRepository.updateById(student);
        return student;
    }

    public void deleteStudent(Long id) {
        Student student = studentRepository.selectById(id);
        if (student == null) {
            throw new RuntimeException("学生不存在");
        }
        // 删除用户
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("related_id", id);
        userRepository.delete(userQueryWrapper);


        // 删除相关预约
        List<Booking> bookings = bookingRepository.findByStudent(student);
        for (Booking booking : bookings) {
            bookingRepository.deleteById(booking.getId());
        }
        
        // 删除相关评价
        List<Review> reviews = reviewRepository.findByStudent(student);
        for (Review review : reviews) {
            reviewRepository.deleteById(review.getId());
        }
        
        studentRepository.deleteById(id);
    }

    public boolean existsByStudentId(String studentId) {
        return studentRepository.existsByStudentId(studentId);
    }

    public boolean existsByEmail(String email) {
        return studentRepository.existsByEmail(email);
    }

    public boolean existsById(Long id) {
        return studentRepository.selectById(id) != null;
    }
}